package com.wannengbang.cloudleader.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.utils.FastClickUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.ViewLoading;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 划拨或分配给门店
 */
public class SetMerchantRateDialog extends DialogFragment {


    @BindView(R.id.edit_min_coding)
    EditText editMinCoding;
    @BindView(R.id.edit_max_coding)
    EditText editMaxCoding;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    @BindView(R.id.tv_current_num)
    TextView tvCurrentNum;
    @BindView(R.id.tv_hight_name)
    TextView tvHightName;
    @BindView(R.id.edit_hight_value)
    EditText editHightValue;
    @BindView(R.id.tv_common_name)
    TextView tvCommonName;
    @BindView(R.id.edit_common_value)
    EditText editCommonValue;
    @BindView(R.id.tv_wxpay_alipay_name)
    TextView tvWxpayAlipayName;
    @BindView(R.id.edit_wxpay_alipay_value)
    EditText editWxpayAlipayValue;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_compre_rate_name)
    TextView tvCompreRateName;
    @BindView(R.id.edit_compre_rate_value)
    EditText editCompreRateValue;
    private Dialog dialog;
    private OnCallBackListener onCallBackListener;
    private Activity mActivity;
    private String agent_id;
    private String store_no;
    private String type;
    private String min_no;
    private String max_no;
    private List<String> store_number_list;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_set_merchant_rate, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        return view;
    }

    public static SetMerchantRateDialog newInstance(String type, List<String> store_number_list) {
        Bundle args = new Bundle();
        SetMerchantRateDialog fragment = new SetMerchantRateDialog();
        args.putString("type", type);
        args.putSerializable("store_number_list", (Serializable) store_number_list);
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    //    初始化请求
    private void initView() {
        type = getArguments().getString("type");
        store_number_list = (List<String>) getArguments().getSerializable("store_number_list");
        if (store_number_list != null) {
            tvCurrentNum.setText(store_number_list.size() + "");
        } else {
            store_number_list = new ArrayList<>();
        }
        llCode.setVisibility(View.GONE);
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.tv_commit})
    public void onViewClicked(View view) {
        if (FastClickUtils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_commit:
                String compre_rate = editCompreRateValue.getText().toString();
                String high_rate = editHightValue.getText().toString();
//                String normal_rate = editCommonValue.getText().toString();
//                String low_rate = editWxpayAlipayValue.getText().toString();

                if (TextUtils.isEmpty(compre_rate) && TextUtils.isEmpty(high_rate)) {
                    ToastUtil.showShort("请输入费率");
                    return;
                }

                if (store_number_list.size() == 0) {
                    ToastUtil.showShort("请选择要设置的机具");
                    return;
                }
                ViewLoading.showProgress(mActivity);
                new HomePageModelImpl().requestDeviceUnifySetStoreRate(compre_rate, high_rate, "", "", store_number_list, new DataCallBack<BaseResponseBean>() {
                    @Override
                    public void onSuccessful(BaseResponseBean bean) {
                        ToastUtil.showShort(bean.getMsg());
                        if (onCallBackListener != null) {
                            onCallBackListener.onCallBack();
                        }
                        dismiss();
                    }

                    @Override
                    public void onFailed(String msg, String code) {

                    }
                });
                break;
        }
    }

    public interface OnCallBackListener {
        void onCallBack();
    }
}
